package com.kuolie.answer.utils;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;

import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

@Configuration
public class MessageConverterConfiguration {
    @Bean
    public FastJsonHttpMessageConverter createMappingMsgConverter() throws Exception{
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new
                FastJsonHttpMessageConverter();

        List<MediaType> supportedMediaTypes = Arrays.asList(new MediaType[]{
                new MediaType(MediaType.TEXT_HTML,Charset.forName("UTF-8")),
                MediaType.APPLICATION_JSON
        });
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
        fastJsonHttpMessageConverter.setFeatures(new SerializerFeature[]{
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.QuoteFieldNames,
                SerializerFeature.WriteMapNullValue
        });
        return fastJsonHttpMessageConverter;
    }
}
